package com.xue.domain;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xue
 * @create 2022-09-21 11:18
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class StaticLocationLightInfo extends Info{

    private transient List<LocationLightInfo> list;

    private String idArray;

    private String lightArray;

    public StaticLocationLightInfo(List<LocationLightInfo> list) {
        this.list = list;
    }

    @Override
    public ByteBuf encoding(ByteBuf byteBuf) {
        this.idArray = list.stream().map(LocationLightInfo::getId).collect(Collectors.joining(","));
        this.lightArray = list.stream().map(item -> item.getLight().toString()).collect(Collectors.joining(","));
        byteBuf.writeByte(Info.MAGIC);
        JSONObject jsonObject = JSONUtil.parseObj(this);
        byte[] bytes = jsonObject.toString().getBytes(StandardCharsets.UTF_8);
        byteBuf.writeShort(bytes.length + Info.FRAME_HEADER);
        byteBuf.writeShort(getInfoType());
        byteBuf.writeBytes(bytes);
        return byteBuf;
    }

    @Override
    public Integer getInfoType() {
        return STATIC_LOCATION_LIGHT_INFO;
    }
}
